package offer.diff;

import java.util.ArrayList;

/**
 * @author DengYuan2
 * @create 2021-03-21 20:07
 */
public class n_62 {
    public static void main(String[] args) {
        n_62 n = new n_62();
        int res = n.LastRemaining_Solution(5, 3);
        System.out.println(res);
    }

    /**
     * 思路：
     * @param n
     * @param m
     * @return
     */
    public int LastRemaining_Solution(int n, int m) {
        if (n==0){
            return -1;
        }
        if (n==1){
            return 0;
        }
        return (LastRemaining_Solution(n-1,m)+m)%n;

    }

    public int LastRemain(int n,int m){
        ArrayList<Integer> list = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            list.add(i);
        }
        int start=-1;
        while (list.size()>0){
            for (int i = 0; i < m; i++) {
                start++;
                if (start==list.size()){
                    start=0;
                }
            }
            list.remove(start);
            start--;
        }


        return -1;
    }
}
